package com.linewell.controller;

import com.linewell.pojo.Book;
import com.linewell.service.BookService;
import com.linewell.util.UUIDUtil;
import com.linewell.util.query.QueryEntity;
import com.linewell.util.query.QueryModel;
import com.linewell.util.query.QueryOperator;
import com.sun.org.apache.xml.internal.resolver.Catalog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: demo
 * @description:
 * @author: hyuhuang@linewell.com
 * @since:2018/4/26
 */
@Controller
public class BookController {

    @Autowired
    BookService bookService;

    /**
     * 插入
     */
    @RequestMapping("/insert")
    public void insertBook(){
        Book book = new Book(UUIDUtil.getUUID(),"ass");
        bookService.insertBook(book);
    }

    /**
     * 根据条件删除
     */
    @RequestMapping("/delete")
    public void deleteBook(){
        String l = "test";
        bookService.deleteBook(l);
    }

    /**
     * 更新数据
     */
    @RequestMapping("update")
    public void updateBook(){
        String updateValue = "更新";
        String id = "1L";
        bookService.update(id,updateValue);
    }

    /**
     * 批量生成数据
     */
    @RequestMapping("insertBooks")
    public void insertBooks(){
        String uuid = "";
        String reader = "";
        for (int i = 0 ; i < 100; i++){
            uuid = UUIDUtil.getUUID();
            reader = "测试" + i;
            Book book = new Book(uuid,reader);
            bookService.insertBook(book);
        }
    }

    /**
     * 分页查询所有图书
     */
    @RequestMapping("findBooks")
    public void findBooks(){
        int size = 5;
        int page = 1;
        bookService.findBooks(page,size);
    }

    /**
     * 多条件查询（排序）无分页
     */
    @RequestMapping("findBookByCon")
    public void findBookByCon(){
        Map<String, QueryEntity> map = new HashMap<String, QueryEntity>();
        //条件一 id=5c8909ff764c4e2eb795557511d01053
        QueryEntity entity = new QueryEntity();
        entity.setField("id");
        entity.setValue(new String[]{"c8d46299d21a4a4d8d054fb555ae5627"});
        entity.setType(new QueryOperator[]{ QueryOperator.IS });
        //条件二
        QueryEntity entity2 = new QueryEntity();
        entity2.setField("reader");
        entity2.setValue(new String[]{"测试1"});
        entity2.setType(new QueryOperator[]{ QueryOperator.IS });
        map.put(entity.getField(),entity);
        map.put(entity2.getField(),entity2);
        //map 多条件   new Integer[]{1} 是排序降序，new String[]{ "createtime" } 按createtime排序
        bookService.findBookByCon(map, new Integer[]{ 1 },new String[]{ "reader" }, QueryModel.AND);
    }

    /**
     * 多条件查询（排序）
     */
    @RequestMapping("findBookSort")
    public void findBookSort(){
        Map<String, QueryEntity> map = new HashMap<String, QueryEntity>();
        QueryEntity entity1 = new QueryEntity();
        entity1.setField("id");
        entity1.setValue(new String[]{"034d844753de499886f2a36b5a2e060c"});
        entity1.setType(new QueryOperator[]{QueryOperator.IS});
        map.put(entity1.getField(),entity1);
        QueryEntity entity2 = new QueryEntity();
        entity2.setField("reader");
        entity2.setValue(new String[]{"测试8"});
        entity2.setType(new QueryOperator[]{ QueryOperator.IS });
        map.put(entity2.getField(),entity2);
        int page = 1;
        int size = 10;
        bookService.findBookSort(map,page,size,new Integer[]{1},new String[]{"reader"},QueryModel.AND);
    }

    /**
     * 多条件统计
     */
    @RequestMapping("countBook")
    public void countBook(){
        Map<String, QueryEntity> map = new HashMap<String, QueryEntity>();
        int page = 1;
        int size = 10;
        bookService.countBook(map,QueryModel.AND);
    }



}
